//使用vue-router插件
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//Part1-引入路由组件
import NavBar from "@/pages/Nav"
import Register from "@/pages/Register"
import Login from "@/pages/Login"
import GoodsList from "@/pages/GoodsLIst"
import Detail from "@/pages/Detail"
import Orders from "@/pages/Orders"

//Part2-定义路由
const routes = [
    {
        path:'/',
        component:NavBar,
        meta:{
            isShowTitle:false
        }
    },
    {
        path:'/register',
        component: Register,
        meta:{
            isShowTitle:true,
            titleText:"注册",
            changeTitleWidth:"1240px",
            changeTitleColor:"#4eb7ff",
        }
    },
    {
        path:'/login',
        component: Login,
        meta:{
            isShowTitle:true,
            titleText:"登录",
            changeTitleWidth:"1240px",
            changeTitleColor:"#4eb7ff",
        }
    },
    {
        path:'/goodsList',
        component: GoodsList,
        meta:{
            isShowTitle:true,
            titleText:"住酒店",
            changeTitleWidth:"1240px",
            changeTitleColor:"#51446e",
        }
    },
    {
        path:"/detail",
        name:"detail",
        component: Detail,
        meta:{
            isShowTitle:true,
            titleText:"详情信息",
            changeTitleWidth:"1240px",
            changeTitleColor:"#ffb508",
        }
    },
    {
        path: "/orders",
        component: Orders,
        meta: {
            isShowTitle:true,
            titleText:"我的订单",
            changeTitleWidth:"1240px",
            changeTitleColor:"#ffb508",
        }
    }
]

//Part3-创建vue-router实例对象
export const router = new VueRouter({
    routes,
    scrollBehavior(to, from, savedPosition){
        return { x:0, y:0 };
    }
})

//配置路由守卫
router.beforeEach((to, from, next)=>{
    let isUser = false;
    if (window.localStorage.getItem("TOKEN")){
        isUser = true;
    }
    // if (isUser){
    //     next();
    // }else{
    //     if (to.path=="/login" || to.path=="/register" || to.path=="/" || to.path=="/goodsList"){
    //         next();
    //     }else{
    //         alert("你还没登录，无法访问。")
    //         next('/login');
    //     }
    // }
    next();

})